app.controller("home",function($scope,$http,$rootScope,$uibModal,get,$state){
	//localStorage.setItem("Url","http://192.168.0.212:8083/")
	localStorage.setItem("Url","http://120.26.222.134:8088/mis/")
	//localStorage.setItem("Url","http://10.37.10.245:8088/mis/")
	$scope.userNames=JSON.parse(localStorage.getItem("user")).staff.sName
	$scope.menu=JSON.parse(localStorage.getItem("privileges"))
	
	$scope.pages={}
	/**
	 * 栏目
	 * @param {Object} val
	 * @param {Object} name
	 */
	$scope.getPage=function(val,name,pages){
		localStorage.setItem("Name",name||'s');
		localStorage.setItem("page",val);
		val=localStorage.getItem("page")
		$scope.page=localStorage.getItem("page")
		if(pages!=null){
			$scope.pages=pages
			$scope.pages[val]=!$scope.pages[val]
			localStorage.setItem("pages",JSON.stringify($scope.pages));
			angular.forEach(pages,function(o,value){
				if(val!=value&localStorage.getItem("Name")=='s'){
					$scope.pages[value]=false
				}
			})
		}else{
			$scope.pages[val]=!$scope.pages[val]
			localStorage.setItem("pages",JSON.stringify($scope.pages));
			angular.forEach($scope.pages,function(o,value){
				if(val!=value&localStorage.getItem("Name")=='s'){
					$scope.pages[value]=false
				}
			})
		}
		/*$state.go(localStorage.getItem("page"))*/
	}
	
	$scope.getPage(localStorage.getItem("page"),localStorage.getItem("Name"),JSON.parse(localStorage.getItem("pages")))
	/**
	 * 怎删改查提示
	 * @param {Object} url
	 */
	$rootScope.open = function(val) {
		setTimeout(function() {
			$uibModal.open({ //模态框
				animation: true,
				templateUrl: "myModalAddFail.html",
				size: 'sm',
				controller: function($uibModalInstance, $timeout,$scope) {
					$scope.val=val
					$timeout(function() {
						$uibModalInstance.close();
					}, 800)
				}
			})
		}, 400)
	}
	$scope.exit=function(){
		localStorage.removeItem("user")
		localStorage.removeItem("page")
		localStorage.removeItem("privileges")
		get.list("login/exit").then(function(e){
			window.location.href="index.html#!/login"
		})
		
	}
	$scope.add=function(){
		$uibModal.open({ //模态框
			animation: true,
			templateUrl: 'userAdd.html',
			size: 'lg',
			 backdrop: 'static' ,
			controller: 'userAdd',
			resolve: { //这是一个入参,这个很重要,它可以把主控制器中的参数传到模态框控制器中
				id: function() { //是一个回调函数
					return JSON.parse(localStorage.getItem("user")).uId; //这个值会被模态框的控制器获取到
				},
				current:function (){
					return $scope.current;
				},
				scope:function(){
					return $scope;
				}
			}
		}) //模态框
	}
})
app.controller('userAdd',function($scope,$rootScope,get,ngTreetableParams,$uibModalInstance,id,$http,post,scope,$state,current,Md5){
	$scope.id=id
	$scope.add={}
	$scope.shows=false
	$scope.current=current
	$scope.sername="修改";
	$scope.user=function(){
		$scope.shows=true
	}
	$scope.password=function(){
		if( $scope.add.password!=null){
			$scope.pwd=true
		}
		
	}
	if($scope.show){
	$scope.$watch("add.user",function  (val) {
			if(!val)return;
				get.list("user/account",{uAccount:val}).then(function(e){
					 $scope.register=e
				})
                 
                })
	}
	var param={uId:$scope.id
		};
		param.diId=$scope.id
		get.list("user/findOne",
			param).then(function(e){
				
				$scope.add.user=e.uAccount
				$scope.add.qpassword=e.uPassword
				$scope.add.password=e.uPassword
		});
	$scope.ok=function (){
		if($scope.pwd){
				$scope.add.password=Md5.hex_md5($scope.add.password)
				$scope.add.qpassword=Md5.hex_md5($scope.add.qpassword)
			}
		console.log($scope.add)
		var param={uId:$scope.id,
			uPassword:$scope.add.password
		};
		param.diId=$scope.id
		post.list("user/pwd",
			param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			$state.go($scope.current.name,null,{
				     		reload:true
				     	})
			
		});
	}
	$scope.cancel = function() {
		$uibModalInstance.close();
	};
	
});